package com.cryptoneedle.center.source.entity;

import com.bubbles.engine.data.core.entity.BaseEntity;
import com.cryptoneedle.center.common.key.ColumnKey;
import jakarta.persistence.*;
import lombok.*;
import lombok.experimental.Accessors;
import org.hibernate.annotations.Comment;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

/**
 * <p>description: Doris-元数据-Column </p>
 * <p>
 * 整合参考：
 * 1.SELECT * FROM information_schema.schemata; 信息完整
 * 2.SHOW FULL COLUMNS FROM internal.xxx.xxx; 不同步
 *
 * @author CryptoNeedle
 * @date 2025-09-25
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(schema = "public", name = "doris_column")
@IdClass(ColumnKey.class)
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Comment("Doris-Column")
public class DorisColumn extends BaseEntity {

    @Id
    @Column(length = 512)
    @Comment("Catalog 名称")
    private String catalogName;

    @Id
    @Column(length = 64)
    @Comment("Database 名称")
    private String databaseName;

    @Id
    @Column(length = 64)
    @Comment("表名称")
    private String tableName;

    @Column(length = 64)
    @Comment("列名称")
    private String columnName;

    @Comment("列在表中所处的位置")
    private Long ordinalPosition;

    @Column(length = 1024)
    @Comment("列的默认值")
    private String columnDefault;

    //@Column(length = 3)
    //@Comment("是否允许为 NULL")
    //private String isNullable;

    @Column(length = 64)
    @Comment("数据类型")
    private String dataType;

    @Comment("字符类型允许的最大字符数")
    private Long characterMaximumLength;

    @Comment("字符类型允许的最大字节数")
    private Long characterOctetLength;

    @Comment("数值类型的 Precision")
    private Long numericPrecision;

    @Comment("数值类型的 Scale")
    private Long numericScale;

    @Comment("datetime 类型的 Precision")
    private Long datetimePrecision;

    //@Column(length = 32)
    //@Comment("字符类型的字符集名称，永远为 NULL")
    //private String characterSetName;

    //@Column(length = 32)
    //@Comment("字符类型的排序算法名称，永远为 NULL")
    //private String collationName;

    @Column(length = 32)
    @Comment("列的类型")
    private String columnType;

    @Column(length = 3)
    @Comment("如果是 UNI，则表示当前列是 Unique Key 列")
    private String columnKey;

    @Column(length = 27)
    @Comment("列的一些额外信息。包括展示是否为自增列，是否为 Generated 列等")
    private String extra;

    //@Column(length = 80)
    //@Comment("永远为空")
    //private String privileges;

    @Column(length = 255)
    @Comment("列的备注信息")
    private String columnComment;

    @Comment("列的宽度")
    private Long columnSize;

    @Comment("数值类型的小数位数")
    private Long decimalDigits;

    //@Column(length = 64)
    //@Comment("永远为 NULL")
    //private String generationExpression;

    //@Comment("永远为 NULL")
    //private Long srsId;

    public ColumnKey getKey() {
        return new ColumnKey(this.catalogName, this.databaseName, this.tableName, this.columnName);
    }

    public String getKeyString() {
        return getKey().toString();
    }
}